package com.sondertara.joya.core;

import com.sondertara.common.collection.Lists;
import com.sondertara.joya.core.jdbc.resultset.mapper.SingleColumnRowMapper;
import com.sondertara.joya.core.jdbc.resultset.mapper.SingleRowRecordExtractor;
import com.sondertara.joya.core.po.TaraJobInstancePo;
import com.sondertara.joya.core.query.NativeSqlQuery;
import com.sondertara.joya.pool.datasource.JoyaDataSource;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;

import java.util.List;

public class JoyaJdbcTest {

    private static JoyaJdbc joyaJdbc;

    @BeforeAll
    static void setUp() {
        JoyaDataSource dataSource = new JoyaDataSource();
        dataSource.setJdbcUrl("jdbc:mysql://128.192.116.79:13306/tara_job?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false&autoReconnect=true&failOverReadOnly=false&autoReconnectForPools=true&allowMultiQueries=true&allowPublicKeyRetrieval=true");
        dataSource.setUsername("root");
        dataSource.setPassword("mysqL#12");
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        joyaJdbc = new JoyaJdbc(dataSource);
    }


    @Test
    void config() {
        System.out.println("config");
    }

    @Test
    void destroy() {
    }

    @Test
    void inTransaction() {
    }

    @Test
    void query() {
        String name = joyaJdbc.query("select name from tara_job_config where id=?", new SingleRowRecordExtractor<>(new SingleColumnRowMapper<>(String.class)), 1);
        Assertions.assertEquals("测试任务", name);
    }

    @Test
    void queryStream() {
    }

    @Test
    void queryList() {
    }

    @Test
    void testQueryList() {
    }

    @Test
    void querySingleValue() {
        String name = joyaJdbc.querySingleValue("select name from tara_job_config where id=?", String.class, 1);
        Assertions.assertEquals("测试任务", name);
    }

    @Test
    void querySingleRow() {
    }


    @Test
    void update() {
        joyaJdbc.execute("update tara_job_instance set job_id =? where id=?", new Object[]{4, 126450649012371471L});
        Integer name = joyaJdbc.querySingleValue("select job_id from tara_job_instance where id=?", Integer.class, 126450649012371471L);
        Assertions.assertEquals(4, name);
    }

    @Test
    @Order(3)
    void testUpdate() {
        TaraJobInstancePo instancePo = new TaraJobInstancePo();
        instancePo.setId(126450649012371472L);
        instancePo.setStatus(4);
        instancePo.setErrorMsg("ERROR");
        joyaJdbc.updateIgnoreNull(instancePo);
        TaraJobInstancePo row = joyaJdbc.querySingleRow("select * from tara_job_instance where id=?", TaraJobInstancePo.class, 126450649012371472L);

        Assertions.assertEquals(5L, row.getJobId());
        Assertions.assertEquals(4, row.getStatus());

        instancePo.setErrorMsg(null);
        joyaJdbc.update(instancePo);

        TaraJobInstancePo row1 = joyaJdbc.querySingleRow("select * from tara_job_instance where id=?", TaraJobInstancePo.class, 126450649012371472L);

        Assertions.assertNull(row1.getErrorMsg());

    }

    @Test
    @Order(2)
    void insert() {
        TaraJobInstancePo instancePo = new TaraJobInstancePo();
        instancePo.setId(126450649012371472L);
        instancePo.setJobId(5L);
        instancePo.setErrorMsg("ERROR");
        joyaJdbc.insert(instancePo);
        TaraJobInstancePo row = joyaJdbc.querySingleRow("select * from tara_job_instance where id=?", TaraJobInstancePo.class, 126450649012371472L);
        Assertions.assertEquals("ERROR", row.getErrorMsg());
    }

    @Test
    void insertAll() {
    }

    @Test
    void updateByCondition() {
    }


    @Test
    void updateAll() {
    }

    @Test
    void execute() {
    }

    @Test
    void startTransaction() {
    }

    @Test
    void commit() {
    }

    @Test
    void rollback() {
    }

    @Test
    void doInTransactionCallback() {
    }

    @Test
    void doWork() {
    }

    @Test
    void doInTransaction() {
    }

    @Test
    void find() {
    }

    @Test
    void findList() {
    }

    @Test
    void findIdExists() {
        List<Long> list = joyaJdbc.findIdExists(TaraJobInstancePo.class, Lists.newArrayList(126450649012371471L, 126450649012371472L, 126450649012371473L));
        Assertions.assertEquals(2, list.size());
    }

    @Test
    void findAllById() {

        List<TaraJobInstancePo> list = joyaJdbc.findAllById(TaraJobInstancePo.class, Lists.newArrayList(126450649012371471L, 126450649012371472L, 126450649012371473L));
        Assertions.assertEquals(2, list.size());
    }

    @Test
    void findById() {

        TaraJobInstancePo instancePo = joyaJdbc.findById(TaraJobInstancePo.class, 126450649012371471L);
        Assertions.assertNotNull(instancePo);
    }

    @Test
    void queryPage() {
    }

    @Test
    void testQueryPage() {
    }

    @Test
    void testFind() {

    }

    @Test
    void testFindList() {
    }

    @Test
    void count() {

        long count = joyaJdbc.count(NativeSqlQuery.builder()
                .select(TaraJobInstancePo::getId)
                .from(TaraJobInstancePo.class)
                .build());
        Assertions.assertTrue(count > 0);
    }

    @Test
    void testFind1() {
    }

    @Test
    void selectList() {
    }

    @Test
    void deleteById() {

        //joyaJdbc.deleteById()
    }

    @Test
    void deleteByCondition() {
    }

    @Test
    @Order(1)
    void delete() {
        joyaJdbc.deleteById(TaraJobInstancePo.class, 126450649012371472L);
    }

    @Test
    void deleteAllById() {
        //joyaJdbc.deleteAllById()
    }

    @Test
    void testInsert() {
    }

    @Test
    void saveIgnoreNull() {
    }

    @Test
    void saveAll() {
    }

    @Test
    void close() {
    }

    @Test
    void save() {
    }

    @Test
    void dropTables() {
    }

    @Test
    void createTables() {
    }

    @Test
    void testConfig() {
    }

    @Test
    void testDestroy() {
    }

    @Test
    void testInTransaction() {
    }

    @Test
    void testQuery() {
    }

    @Test
    void testQueryStream() {
    }

    @Test
    void testQueryList1() {
    }

    @Test
    void testQueryList2() {
    }

    @Test
    void testQuerySingleValue() {
    }

    @Test
    void testQuerySingleRow() {
    }

    @Test
    void testQuerySingleRow1() {
    }

    @Test
    void testUpdate1() {
    }

    @Test
    void testUpdate2() {
    }

    @Test
    void testInsert1() {
    }

    @Test
    void testInsertAll() {
    }

    @Test
    void testUpdateByCondition() {
    }

    @Test
    void updateIgnoreNull() {
    }

    @Test
    void testUpdate3() {
    }

    @Test
    void testUpdateAll() {
    }

    @Test
    void testExecute() {
    }

    @Test
    void testStartTransaction() {
    }

    @Test
    void testCommit() {
    }

    @Test
    void testRollback() {
    }

    @Test
    void testDoInTransactionCallback() {
    }

    @Test
    void testDoWork() {
    }

    @Test
    void testDoInTransaction() {
    }

    @Test
    void testFind2() {
    }

    @Test
    void testFindList1() {
    }

    @Test
    void testFindIdExists() {
    }

    @Test
    void testFindAllById() {
    }

    @Test
    void testSelectByKey() {
    }

    @Test
    void testQueryPage1() {
    }

    @Test
    void testQueryPage2() {
    }

    @Test
    void testQueryPage3() {
    }

    @Test
    void testQueryPage4() {
    }

    @Test
    void testFind3() {
    }

    @Test
    void testFindList2() {
    }

    @Test
    void testCount() {
    }

    @Test
    void testFind4() {
    }

    @Test
    void testSelectList() {
        NativeSqlQuery query = NativeSqlQuery.builder()
                .select()
                .from(TaraJobInstancePo.class)
                .where(w -> w.eq(TaraJobInstancePo::getId, 126450649012371472L))
                .build();
        List<TaraJobInstancePo> list = joyaJdbc.queryList(query.toSql(), TaraJobInstancePo.class, query.getParams().toArray());
        Assertions.assertNotNull(list);
    }

    @Test
    void testDeleteByKey() {
    }

    @Test
    void testDeleteByCondition() {
    }

    @Test
    void testDelete() {
    }

    @Test
    void testDeleteBatchKeys() {
    }

    @Test
    void insertIgnoreNull() {
    }

    @Test
    void testInsert2() {
    }

    @Test
    void testSaveIgnoreNull() {
    }

    @Test
    void testSaveAll() {
    }

    @Test
    void testClose() {
    }

    @Test
    void runScript() {
    }

    @Test
    void testSave() {
    }

    @Test
    void testDropTables() {
    }

    @Test
    void testCreateTables() {
    }

    @Test
    void createPreparedStatement() {
    }
}